package br.composer.handlers.handlers;

import java.util.ArrayList;

import br.composer.core.DelayMeasurement;
import br.composer.core.Measurement;
import br.composer.handlers.requests.DelayHandlerRequest;
import br.composer.handlers.responses.HandlerResponse;
import br.composer.util.ConsoleOutput;
import br.composer.util.TestOutput;
import owamp.HADESRequest;
import owamp.HADESRequestVO;
import owamp.HADESResultVO;
import util.IConstantsOwamp;


public class HADESDelayHandler implements DelayHandler {

	private TestOutput output;
	private String serviceAddress;
	
	public HADESDelayHandler(TestOutput output, String serviceAddress) {
		super();
		this.output = output;
		this.serviceAddress = serviceAddress;
	}

	public HandlerResponse[] getResponse(DelayHandlerRequest[] requests)
			throws InterruptedException {

		int qtdTests = requests.length;
		HandlerResponse responses[] = new HandlerResponse[qtdTests]; 
		HADESRequest hadesRequests[] =  new HADESRequest[qtdTests]; 
		Thread pending[]  = new Thread[qtdTests];
		
		//for each request...
		for (int i=0;i<qtdTests;i++){
			output.addMsg("***Starting HADES MA Request:  "+ (i + 1)
					+ " of " + (qtdTests) + ". " + requests[i].getSrc()
					+ " ---> " + requests[i].getDst());
			
			HADESRequestVO vo = new HADESRequestVO(serviceAddress,
					requests[i].getSrc(),requests[i].getDst(),
					requests[i].getTos(),requests[i].getPacketSize(),requests[i].getGroupSize(),
					requests[i].getInterval(),requests[i].getPrecedence(),
					requests[i].getEventType1(),requests[i].getEventType2(),
					requests[i].getStartTime(),requests[i].getEndTime());
			System.out.println("Using service address: "+vo.getEndpoint());
			
			hadesRequests[i] = new HADESRequest(vo, IConstantsOwamp.TYPE_DELAY);
			pending[i] = new Thread(hadesRequests[i]);
			pending[i].start();
		}
			
		//wait for test's end
		for (int i = 0; i < qtdTests; i++) {
			if (pending[i].isAlive()){
				pending[i].join();
			}
		}			
		
		for (int i = 0; i < qtdTests; i++) {
			responses[i] = new HandlerResponse();
		
			//if the response is null, report fail 
			if (hadesRequests[i].getResultList()==null){
				output.addMsg("Test "+
						(i+1)+"["+requests[i].getSrc()+"->"+requests[i].getDst()+"]"+" failed");
				responses[i].setFailed(true);
			} else { 
				//create list of delaySample's
				responses[i].setFailed(false);
				
				for (HADESResultVO vo:hadesRequests[i].getResultList()){
					DelayMeasurement m = new DelayMeasurement();
					m.setDuplicates(vo.getDuplicates());
					m.setLoss(vo.getLoss());
					m.setMaxDelay(vo.getMaxDelay());
					m.setMedDelay(vo.getMedDelay());
					m.setMinDelay(vo.getMinDelay());
					m.setTime(vo.getTime());
					responses[i].getResults().add(m); //TODO verificar se funciona
				}
			}
		}
		
		return responses;
	}
	
	public static void main(String[] args) {
		HADESDelayHandler handler = new HADESDelayHandler(new ConsoleOutput(), 
				"http://radagast.x-win.dfn.de:8090/services/MA/HADES/GEANT");
		
		DelayHandlerRequest requests[] = new DelayHandlerRequest[1];
		requests[0] = new DelayHandlerRequest("London_GEANT","Athens_GEANT");
		requests[0].setStartTime(1292198400);
		requests[0].setEndTime(1292284799);
		requests[0].setTos("0x0");
		requests[0].setPacketSize(41);
		requests[0].setGroupSize(9);
		requests[0].setInterval(60);
		requests[0].setPrecedence("0x0");
		requests[0].setEventType1("aggregated");
		requests[0].setEventType2("select");
		
		try {
			HandlerResponse resp = handler.getResponse(requests)[0];
			
			for (Measurement res:resp.getResults()){
				System.out.println( ((DelayMeasurement)res).getMedDelay());
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public String getNome() {
		return "Hades MA Delay Handler";
	}
}
